package com.yx.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yx.constatns.RabbitMQConstatns;
import com.yx.feign.api.OrderService;
import com.yx.services.IStockService;
import com.yx.vo.Order;
import com.yx.vo.OrderDetail;
import com.yx.vo.StockParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderListener
 * @Date 2021/07/26 21:29
 * @Author by yx
 * @Description TODO:
 */
@Component
@Slf4j
public class StockListener {

    @Autowired
    private OrderService orderService;
    @Autowired
    private IStockService stockService;

    @RabbitListener(queues = RabbitMQConstatns.STOCK_DLE_QUEUE)
    public void orderUpdateStatus(String json, Channel channel, Message message) {
        try {
            Map dleMap = JSON.parseObject(json, Map.class);
            log.debug("====>{}", dleMap);
            String string = JSON.toJSONString(dleMap.get("orderDetailList"));
            List<OrderDetail> orderDetailList = JSON.parseArray(string, OrderDetail.class);
            Map<String,Object> orderMap = (Map<String, Object>) dleMap.get("order");
            Order order = JSON.parseObject(JSON.toJSONString(orderMap), Order.class);
            // 查询订单最新状态
            Order orderByOid = orderService.getOrderByOid(order.getOid());
            log.debug("订单最新状态:{}", orderByOid);
            switch (orderByOid.getStatus()) {
                case 1: // 未支付
                    log.debug("2M了，订单还是未支付，释放库存");
                    unLockStock(orderDetailList);
                    break;
                case 3: // 已取消
                    log.debug("2M了，订单已取消，释放库存");
                    unLockStock(orderDetailList);
                    break;
                case 4: // 已超时了
                    log.debug("2M了，订单超时了，释放库存");
                    unLockStock(orderDetailList);
                    break;
                case 2: // 已支付
                    log.debug("订单已支付,不需要处理");
                    break;
                default:
                    break;
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*
     * 释放库存 根据oid与商品数量
     * */
    private void unLockStock(List<OrderDetail> orderDetailList) {

        List<StockParam> stockParamList = new ArrayList<>();

        for (OrderDetail orderDetail : orderDetailList) {
            Integer sum = orderDetail.getSum();
            Integer gid = orderDetail.getGid();

            // 判断发货仓库
            StockParam stockParam = new StockParam();
            stockParam.setSum(sum);
            stockParam.setGid(gid);
            stockParam.setWid(1);

            stockParamList.add(stockParam);
        }
        stockService.unLockGoodsStock(stockParamList);
    }
}
